module AboutYou
  module SDK
    module Model
      ###
      # This class represents a facet group model
      ###
      class FacetGroup
        # Array of instances of AboutYou::SDK::Model::Facet
        attr_accessor :facets
        # id of this facet group
        attr_accessor :id
        # name of the facet group
        attr_accessor :name
        # id of the group of this facet group
        attr_accessor :group_id

        ###
        # Constructor for the AboutYou::SDK::Model::FacetGroup class
        #
        # * *Args* :
        #   - +id+ -> the id of the facet group
        #   - +name+ -> the name of the facet group
        #
        # * *Returns* :
        #   - an instance of AboutYou::SDK::Model::FacetGroup
        ###
        def initialize(id, name)
          self.id = id
          self.name = name
          self.facets = {}

          self
        end

        ###
        # this method adds a facet to the facet group
        #
        # * *Args* :
        #   - +facet+ -> instance of AboutYou::SDK::Model::Facet which should be added
        ###
        def add_facet(facet)
          facets[facet.id] = facet
        end

        ###
        # this method adds facets to the facet group
        #
        # * *Args* :
        #   - +facets+ -> Array containing instances of AboutYou::SDK::Model::Facet which should be added
        ###
        def add_facets(facets)
          facets.each do |facet|
            add_facet(facet)
          end
        end

        ###
        # this method is used for getting all of the facet names of this facet group
        #
        # * *Args* :
        #   - +separator+ -> String controlling how the facet names should be seperated
        #   
        # * *Returns* :
        #   - Array of Strings
        ###
        def facet_names(separator = '/')
          names = []
          facets.each do |facet|
            names.push(facet.name)
          end
          names.join(separator)

          names
        end

        ###
        # This method is used for checking whether a given facet group is equal
        # to this facet group
        #
        # * *Args* :
        #   - +facet_group+ -> instance of AboutYou::SDK::Model::FacetGroup used for checking
        #   
        # * *Returns* :
        #   -Boolean determining whether the facet groups are equal
        ###
        def equal?(facet_group)
          return false unless id == facet_group.id

          unique_key == facet_group.unique_key
        end

        ###
        # This method is used for creating a unique key for this facet group
        #
        # * *Returns* :
        #   - a String containing a unique key for this facet group
        ###
        def unique_key
          facet_ids = facets.keys
          facet_ids.sort!

          String(id) + ':' + String(facet_ids.join(','))
        end

        ###
        # Gett for the ids
        #
        # * *Returns* :
        #   - Hash containing a pair of facet_group_id => Array of facet_ids
        ###
        def ids
          { id => facets.keys }
        end

        ###
        # This method checks if this facet group contains a facet
        #
        # * *Args* :
        #   - +facet+ -> instance of AboutYou::SDK::Model::Facet
        #
        # * *Returns* :
        #   - Boolean determining whether this facet group contains the facet or not
        ###
        def contains(facet)
          facets.key?(facet.id)
        end
      end
    end
  end
end
